<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div class="img">
                <img src="../assets/logome.png" alt="" />
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">推出</el-button>
        </el-header>
        <!-- 页面主区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width= "isCollapse ? '64px' : '200px'" >
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!-- 侧边栏菜单区域 -->
                <el-menu default-active="2" background-color="#333744" text-color="#fff" active-text-color="#409EFF"
                    unique-opened :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath" >
                    <!-- 一级菜单 -->
                    <el-submenu v-for="item in menulist" :index="item.id+''" :key="item.id">
                        <!-- 一级菜单的模板区 -->
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconsObj[item.id]"></i>
                            <!-- 文本 -->
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item index="1-4-1" v-for="subItem in item.children" :index="'/'+subItem.path"
                            :key="subItem.id" @click="saveNavState('/' + subItem.path)">
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 文本 -->
                                <span>{{subItem.authName}}</span>
                            </template>
                        </el-menu-item>
                        <!-- <el-submenu index="1-4">
                            <template slot="title">选项4</template>

                        </el-submenu> -->
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 页面右主区 -->
            <el-main>
<!--占位符-->
               <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>
<script>
export default {
    data() {
        return {
            menulist: [],  //左侧菜单数据
            iconsObj: {
                '125': 'iconfont icon-user',
                '103': 'iconfont icon-tijikongjian',
                '101': 'iconfont icon-shangpin',
                '102': 'iconfont icon-danju',
                '145': 'iconfont icon-baobiao',

            },
            isCollapse:false,
            // 保持菜单点击的链接 activePath 状态
            activePath:''

        }
    },
    created() {
        // 一打开网页的时候调左侧菜单接口显示菜单
        this.getMenuList();
        // 左侧菜单点击高亮状态保持
        // 菜单点击链接activepath状态 一打开页面就从sessioan哪里拿到，直接动态绑定 el-menu 中的default-active的地址
        this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
        logout() {
            // console.log("1");
            window.sessionStorage.clear();
            this.$router.push("/login");
        },
        //  获取所以的菜单
        async getMenuList() {
            const { data: res } = await this.$http.get('menus')
            // console.log(res);
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.menulist = res.data
        },
        // 点击按钮展开折叠侧边栏菜单=左侧菜单点击高亮状态保持
        toggleCollapse(){
           this.isCollapse = !this.isCollapse;

        },
        // 保存点击二级菜单的激活列表 保持菜单点击的链接 activePath 状态=左侧菜单点击高亮状态保持
        saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
    },
};
</script>
<style lang="less" scoped>
.home-container {
    height: 100%;
}

.el-header {
    background: #272c3b;
   //background:linear-gradient( to left, #ad84b7, #83b7ae, #9784b2); //背景渐变

   display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: white;
    font-size: 20px;

    >div {
        display: flex;
        align-items: center;

        span {
            margin-left: 22px;
        }
    }
}
.toggle-button{
    background: #434757;
    color: #eaedf1;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    // font-weight: bold;
    cursor: pointer;

}
.toggle-button:hover{
    background:#515669;}

.el-menu {
    border-right: none;
}

.el-aside {
    background: #333744;
}

.el-main {
    background: #eaedf1;
}

.iconfont {
    margin-right: 5px;
}
.img img {
   width: 61px;
   height: 56px;
   border-radius: 50%;
}
</style>
